package com.droidclaimsurveyor.dal;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.droidclaimsurveyor.model.ClaimInfoModel;

public class ClaimInfoDal implements DAL <ClaimInfoModel, String>{
	public static String GetAllClaimInfoId = "GetAllClaimInfo";
	public static String GetLastClaimInfoId = "GetLastClaimInfo";
	public static String GetClaimInfoByClaimId = "GetClaimInfoByClaimId";
	public static String GetClaimInfoByRowId = "GetClaimInfoByRowId";
	
	private ClaimInfoDBHelper dbHelper;
	private SQLiteDatabase db;
	private Context context;

	public ClaimInfoDal(Context context) {
		this.context = context;
		dbHelper = new ClaimInfoDBHelper(context,
				ClaimInfoDBHelper.DATABASE_NAME, null,
				ClaimInfoDBHelper.DATABASE_VERSION);
	}
	
	public List<ClaimInfoModel> getAll(String taskId,String... parameters){
		List<ClaimInfoModel> result = null;
		if(taskId.equals(ClaimInfoDal.GetAllClaimInfoId)){
			result = getAllClaimInfo(parameters);
		}else if(taskId.equals(ClaimInfoDal.GetLastClaimInfoId)){
			result = getLastClaimInfo();
		}else if(taskId.equals(ClaimInfoDal.GetClaimInfoByClaimId)){
			result = getAllClaimInfoByClaimId(parameters);
		}else if(taskId.equals(ClaimInfoDal.GetClaimInfoByRowId)){
			result = getAllClaimInfoByRowId(parameters);
		}
		
		
		return result;
	}
	
	public List<ClaimInfoModel> getAllClaimInfo(String... licensePlates){
		
		List<ClaimInfoModel> result = new ArrayList<ClaimInfoModel>();
		try{
			String licensePlatId = licensePlates[0];
			String orderBy = ClaimInfoDBHelper.KEY_ACC_DATE + " DESC";
			
			Cursor cursor = getCursorByLicensePlate(licensePlatId, orderBy);
			if (cursor.moveToFirst()) {
				do {
					String dblicensePlateNo = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_LICENSEPLATENO));
					if(licensePlatId.equals(dblicensePlateNo)){
						ClaimInfoModel model = new ClaimInfoModel();
						
						SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
						
						long rowId = cursor.getLong(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ID));
						long id = cursor.getLong(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CLAIM_ID));
						String accDateStr = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_DATE));
						Date accDate = df.parse(accDateStr);
						String accLocation = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LOCATION));
						Double latitude = cursor.getDouble(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LATITUDE));
						Double longitude = cursor.getDouble(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LONGITUDE));
						String updateDateStr = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_UPDATE_DATE));
						Date updateDate = df.parse(updateDateStr);						
						int claimDocId = cursor.getInt(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CLAIM_DOC_ID));
						String licensePlateNo = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_LICENSEPLATENO));
						String custName = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_NAME));
						String custImgPath = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_IMAGE));
						String carColor = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_COLOR));
						String carBrand = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_BRAND));
						String carDetail = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_DETAIL));
						String insurancePackage = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE));
						String custMobile = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_MOBILE));
						
						model.setRowId(rowId);
						model.setAccDate(accDate);
						model.setAccLocation(accLocation);
						model.setId(id);
						model.setLatitude(latitude);
						model.setLongitude(longitude);
						model.setUpdateDate(updateDate);
						model.setLicensePlateNo(dblicensePlateNo);
						model.setClaimDocId(claimDocId);
						model.setLicensePlateNo(licensePlateNo);
						model.setCustName(custName);
						model.setCustImgPath(custImgPath);
						model.setCarColor(carColor);
						model.setCarBrand(carBrand);
						model.setCarDetail(carDetail);
						model.setInsurancePackage(insurancePackage);
						model.setCustMobile(custMobile);
						
						result.add(model);
					}
				} while (cursor.moveToNext());
			}		
		}catch(Exception ex){
		}
		
		return result;
	}
	
	public List<ClaimInfoModel> getAllClaimInfoByClaimId(String... cliamIdStr){
		
		List<ClaimInfoModel> result = new ArrayList<ClaimInfoModel>();
		try{
			String cliamId = cliamIdStr[0];
			Cursor cursor = getCursorByClaimId(cliamId);
			if (cursor.moveToFirst()) {
				do {
					long dbClaimId = cursor.getLong(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CLAIM_ID));
					if(cliamIdStr.equals(Long.toString(dbClaimId))){
						ClaimInfoModel model = new ClaimInfoModel();
						
						SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
						
						long rowId = cursor.getLong(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ID));
						long id = cursor.getLong(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CLAIM_ID));
						String accDateStr = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_DATE));
						Date accDate = df.parse(accDateStr);
						String accLocation = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LOCATION));
						Double latitude = cursor.getDouble(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LATITUDE));
						Double longitude = cursor.getDouble(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LONGITUDE));
						String updateDateStr = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_UPDATE_DATE));
						Date updateDate = df.parse(updateDateStr);						
						int claimDocId = cursor.getInt(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CLAIM_DOC_ID));
						String licensePlateNo = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_LICENSEPLATENO));
						String custName = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_NAME));
						String custImgPath = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_IMAGE));
						String carColor = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_COLOR));
						String carBrand = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_BRAND));
						String carDetail = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_DETAIL));
						String insurancePackage = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE));
						String custMobile = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_MOBILE));
						
						model.setRowId(rowId);
						model.setAccDate(accDate);
						model.setAccLocation(accLocation);
						model.setId(id);
						model.setLatitude(latitude);
						model.setLongitude(longitude);
						model.setUpdateDate(updateDate);
						model.setLicensePlateNo(licensePlateNo);
						model.setClaimDocId(claimDocId);
						model.setLicensePlateNo(licensePlateNo);
						model.setCustName(custName);
						model.setCustImgPath(custImgPath);
						model.setCarColor(carColor);
						model.setCarBrand(carBrand);
						model.setCarDetail(carDetail);
						model.setInsurancePackage(insurancePackage);
						model.setCustMobile(custMobile);
						
						result.add(model);
					}
				} while (cursor.moveToNext());
			}		
		}catch(Exception ex){
		}
		
		return result;
	}

	public List<ClaimInfoModel> getAllClaimInfoByRowId(String... rowIdStr){
		
		List<ClaimInfoModel> result = new ArrayList<ClaimInfoModel>();
		try{
			String rowId = rowIdStr[0];
			Cursor cursor = getCursorByClaimId(rowId);
			if (cursor.moveToFirst()) {
				do {
					long dbRowId = cursor.getLong(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ID));
					if(rowIdStr.equals(Long.toString(dbRowId))){
						ClaimInfoModel model = new ClaimInfoModel();
						
						SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
						
						long id = cursor.getLong(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CLAIM_ID));
						String accDateStr = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_DATE));
						Date accDate = df.parse(accDateStr);
						String accLocation = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LOCATION));
						Double latitude = cursor.getDouble(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LATITUDE));
						Double longitude = cursor.getDouble(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LONGITUDE));
						String updateDateStr = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_UPDATE_DATE));
						Date updateDate = df.parse(updateDateStr);						
						int claimDocId = cursor.getInt(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CLAIM_DOC_ID));
						String licensePlateNo = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_LICENSEPLATENO));
						String custName = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_NAME));
						String custImgPath = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_IMAGE));
						String carColor = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_COLOR));
						String carBrand = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_BRAND));
						String carDetail = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_DETAIL));
						String insurancePackage = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE));
						String custMobile = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_MOBILE));
						
						model.setRowId(dbRowId);
						model.setAccDate(accDate);
						model.setAccLocation(accLocation);
						model.setId(id);
						model.setLatitude(latitude);
						model.setLongitude(longitude);
						model.setUpdateDate(updateDate);
						model.setLicensePlateNo(licensePlateNo);
						model.setClaimDocId(claimDocId);
						model.setLicensePlateNo(licensePlateNo);
						model.setCustName(custName);
						model.setCustImgPath(custImgPath);
						model.setCarColor(carColor);
						model.setCarBrand(carBrand);
						model.setCarDetail(carDetail);
						model.setInsurancePackage(insurancePackage);
						model.setCustMobile(custMobile);
						
						result.add(model);
					}
				} while (cursor.moveToNext());
			}		
		}catch(Exception ex){
		}
		
		return result;
	}
	
	public List<ClaimInfoModel> getLastClaimInfo(){
		
		List<ClaimInfoModel> result = new ArrayList<ClaimInfoModel>();
		try{
			Cursor cursor = getLastRecord();
			if (cursor.moveToFirst()) {
				do {
					String dblicensePlateNo = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_LICENSEPLATENO));
					ClaimInfoModel model = new ClaimInfoModel();
											
					SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
					
					long rowId = cursor.getLong(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ID));
					long id = cursor.getLong(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CLAIM_ID));
					String accDateStr = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_DATE));
					Date accDate = (accDateStr != null ? df.parse(accDateStr) : null);
					String accLocation = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LOCATION));
					Double latitude = cursor.getDouble(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LATITUDE));
					Double longitude = cursor.getDouble(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_ACC_LONGITUDE));
					String updateDateStr = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_UPDATE_DATE));
					Date updateDate = (updateDateStr != null ? df.parse(updateDateStr) : null);						
					int claimDocId = cursor.getInt(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CLAIM_DOC_ID));
					String licensePlateNo = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_LICENSEPLATENO));
					String custName = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_NAME));
					String custImgPath = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_IMAGE));
					String carColor = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_COLOR));
					String carBrand = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_BRAND));
					String carDetail = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CAR_DETAIL));
					String insurancePackage = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE));
					String custMobile = cursor.getString(cursor.getColumnIndex(ClaimInfoDBHelper.KEY_CUST_MOBILE));
					
					model.setRowId(rowId);
					model.setAccDate(accDate);
					model.setAccLocation(accLocation);
					model.setId(id);
					model.setLatitude(latitude);
					model.setLongitude(longitude);
					model.setUpdateDate(updateDate);
					model.setLicensePlateNo(dblicensePlateNo);
					model.setClaimDocId(claimDocId);
					model.setLicensePlateNo(licensePlateNo);
					model.setCustName(custName);
					model.setCustImgPath(custImgPath);
					model.setCarColor(carColor);
					model.setCarBrand(carBrand);
					model.setCarDetail(carDetail);
					model.setInsurancePackage(insurancePackage);
					model.setCustMobile(custMobile);
					
					result.add(model);
				} while (cursor.moveToNext());
			}		
		}catch(Exception ex){
		}
		
		return result;
	}	
	
	public void close() {
		dbHelper.close();
	}

	public void open() {
		db = null;
		try {
			db = dbHelper.getWritableDatabase();
		} catch (Exception ex) {
			db = dbHelper.getReadableDatabase();
		}
	}

	public long add(ClaimInfoModel claimInfo) {
		long rowIndex = -1;

		try {
			SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
			
			ContentValues data = new ContentValues();
			
			data.put(ClaimInfoDBHelper.KEY_CLAIM_ID, claimInfo.getId());
			data.put(ClaimInfoDBHelper.KEY_ACC_DATE, df.format(claimInfo.getAccDate()));
			data.put(ClaimInfoDBHelper.KEY_ACC_LOCATION,claimInfo.getAccLocation());
			data.put(ClaimInfoDBHelper.KEY_ACC_LATITUDE,claimInfo.getLatitude());
			data.put(ClaimInfoDBHelper.KEY_ACC_LONGITUDE,claimInfo.getLongitude());
			data.put(ClaimInfoDBHelper.KEY_UPDATE_DATE, (claimInfo.getUpdateDate() != null ? df.format(claimInfo.getUpdateDate()) : null));
			data.put(ClaimInfoDBHelper.KEY_CLAIM_DOC_ID, claimInfo.getClaimDocId());
			data.put(ClaimInfoDBHelper.KEY_LICENSEPLATENO,claimInfo.getLicensePlateNo());
			data.put(ClaimInfoDBHelper.KEY_CUST_NAME, claimInfo.getCustName());
			data.put(ClaimInfoDBHelper.KEY_CUST_MOBILE,claimInfo.getCustMobile());
			data.put(ClaimInfoDBHelper.KEY_CUST_IMAGE, claimInfo.getCustImgPath());
			data.put(ClaimInfoDBHelper.KEY_CAR_COLOR, claimInfo.getCarColor());
			data.put(ClaimInfoDBHelper.KEY_CAR_BRAND, claimInfo.getCarBrand());
			data.put(ClaimInfoDBHelper.KEY_CAR_DETAIL, claimInfo.getCarDetail());
			data.put(ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE, claimInfo.getInsurancePackage());

			// return row id to caller
			rowIndex = db.insert(ClaimInfoDBHelper.DATABASE_TABLE, null, data);
		} catch (Exception ex) {
			Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
		}

		return rowIndex;
	}

	public boolean update(long rowIndex, ClaimInfoModel claimInfo) {
		SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		
		ContentValues newValue = new ContentValues();
		
		newValue.put(ClaimInfoDBHelper.KEY_CLAIM_ID, claimInfo.getId());
		newValue.put(ClaimInfoDBHelper.KEY_ACC_DATE, df.format(claimInfo.getAccDate()));
		newValue.put(ClaimInfoDBHelper.KEY_ACC_LOCATION,claimInfo.getAccLocation());
		newValue.put(ClaimInfoDBHelper.KEY_ACC_LATITUDE,claimInfo.getLatitude());
		newValue.put(ClaimInfoDBHelper.KEY_ACC_LONGITUDE,claimInfo.getLongitude());
		newValue.put(ClaimInfoDBHelper.KEY_UPDATE_DATE, (claimInfo.getUpdateDate() != null ? df.format(claimInfo.getUpdateDate()) : null));
		newValue.put(ClaimInfoDBHelper.KEY_CLAIM_DOC_ID, claimInfo.getClaimDocId());
		newValue.put(ClaimInfoDBHelper.KEY_LICENSEPLATENO,claimInfo.getLicensePlateNo());
		newValue.put(ClaimInfoDBHelper.KEY_CUST_NAME, claimInfo.getCustName());
		newValue.put(ClaimInfoDBHelper.KEY_CUST_MOBILE,claimInfo.getCustMobile());
		newValue.put(ClaimInfoDBHelper.KEY_CUST_IMAGE, claimInfo.getCustImgPath());
		newValue.put(ClaimInfoDBHelper.KEY_CAR_COLOR, claimInfo.getCarColor());
		newValue.put(ClaimInfoDBHelper.KEY_CAR_BRAND, claimInfo.getCarBrand());
		newValue.put(ClaimInfoDBHelper.KEY_CAR_DETAIL, claimInfo.getCarDetail());
		newValue.put(ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE, claimInfo.getInsurancePackage());
		
		return db.update(ClaimInfoDBHelper.DATABASE_TABLE, newValue, ClaimInfoDBHelper.KEY_ID + "=" + rowIndex,
				null) > 0;
	}

	public boolean remove(long index) {
		return db.delete(ClaimInfoDBHelper.DATABASE_TABLE,
				ClaimInfoDBHelper.KEY_CLAIM_ID + "=" + index, null) > 0;
	}

	public Cursor getAllCursor(String orderBy) {
		String columnName[] = new String[] { ClaimInfoDBHelper.KEY_ID,
				ClaimInfoDBHelper.KEY_CLAIM_ID,
				ClaimInfoDBHelper.KEY_ACC_DATE,
				ClaimInfoDBHelper.KEY_ACC_LOCATION,
				ClaimInfoDBHelper.KEY_ACC_LATITUDE,
				ClaimInfoDBHelper.KEY_ACC_LONGITUDE,
				ClaimInfoDBHelper.KEY_UPDATE_DATE,
				ClaimInfoDBHelper.KEY_CLAIM_DOC_ID,
				ClaimInfoDBHelper.KEY_LICENSEPLATENO,
				ClaimInfoDBHelper.KEY_CUST_NAME,
				ClaimInfoDBHelper.KEY_CUST_MOBILE,
				ClaimInfoDBHelper.KEY_CUST_IMAGE, 
				ClaimInfoDBHelper.KEY_CAR_COLOR, 
				ClaimInfoDBHelper.KEY_CAR_BRAND, 
				ClaimInfoDBHelper.KEY_CAR_DETAIL, 
				ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE
				};

		return db.query(ClaimInfoDBHelper.DATABASE_TABLE, columnName, null,
				null, null, null, orderBy);
	}
	
	public Cursor getCursorByLicensePlate(String licensePlate, String orderBy){
		String columnName[] = new String[] { ClaimInfoDBHelper.KEY_ID,
				ClaimInfoDBHelper.KEY_CLAIM_ID,
				ClaimInfoDBHelper.KEY_ACC_DATE,
				ClaimInfoDBHelper.KEY_ACC_LOCATION,
				ClaimInfoDBHelper.KEY_ACC_LATITUDE,
				ClaimInfoDBHelper.KEY_ACC_LONGITUDE,
				ClaimInfoDBHelper.KEY_UPDATE_DATE,
				ClaimInfoDBHelper.KEY_CLAIM_DOC_ID,
				ClaimInfoDBHelper.KEY_LICENSEPLATENO,
				ClaimInfoDBHelper.KEY_CUST_NAME,
				ClaimInfoDBHelper.KEY_CUST_MOBILE,
				ClaimInfoDBHelper.KEY_CUST_IMAGE, 
				ClaimInfoDBHelper.KEY_CAR_COLOR, 
				ClaimInfoDBHelper.KEY_CAR_BRAND, 
				ClaimInfoDBHelper.KEY_CAR_DETAIL, 
				ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE
				};
		
		String selection = ClaimInfoDBHelper.KEY_LICENSEPLATENO + " = ?";
		String[] selectionArgs = new String[] {licensePlate};
		return db.query(ClaimInfoDBHelper.DATABASE_TABLE, columnName, selection,selectionArgs, null, null, orderBy);
	}

	public Cursor getCursorByClaimId(String clainIdStr){
		String columnName[] = new String[] { ClaimInfoDBHelper.KEY_ID,
				ClaimInfoDBHelper.KEY_CLAIM_ID,
				ClaimInfoDBHelper.KEY_ACC_DATE,
				ClaimInfoDBHelper.KEY_ACC_LOCATION,
				ClaimInfoDBHelper.KEY_ACC_LATITUDE,
				ClaimInfoDBHelper.KEY_ACC_LONGITUDE,
				ClaimInfoDBHelper.KEY_UPDATE_DATE,
				ClaimInfoDBHelper.KEY_CLAIM_DOC_ID,
				ClaimInfoDBHelper.KEY_LICENSEPLATENO,
				ClaimInfoDBHelper.KEY_CUST_NAME,
				ClaimInfoDBHelper.KEY_CUST_MOBILE,
				ClaimInfoDBHelper.KEY_CUST_IMAGE, 
				ClaimInfoDBHelper.KEY_CAR_COLOR, 
				ClaimInfoDBHelper.KEY_CAR_BRAND, 
				ClaimInfoDBHelper.KEY_CAR_DETAIL, 
				ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE
			};
		
		String selection = ClaimInfoDBHelper.KEY_CLAIM_ID + " = ?";
		String[] selectionArgs = new String[] {clainIdStr};
		return db.query(ClaimInfoDBHelper.DATABASE_TABLE, columnName, selection,selectionArgs, null, null, null);
	}
	
	public Cursor getCursorByRowId(String rowIdStr){
		String columnName[] = new String[] { ClaimInfoDBHelper.KEY_ID,
				ClaimInfoDBHelper.KEY_CLAIM_ID,
				ClaimInfoDBHelper.KEY_ACC_DATE,
				ClaimInfoDBHelper.KEY_ACC_LOCATION,
				ClaimInfoDBHelper.KEY_ACC_LATITUDE,
				ClaimInfoDBHelper.KEY_ACC_LONGITUDE,
				ClaimInfoDBHelper.KEY_UPDATE_DATE,
				ClaimInfoDBHelper.KEY_CLAIM_DOC_ID,
				ClaimInfoDBHelper.KEY_LICENSEPLATENO,
				ClaimInfoDBHelper.KEY_CUST_NAME,
				ClaimInfoDBHelper.KEY_CUST_MOBILE,
				ClaimInfoDBHelper.KEY_CUST_IMAGE, 
				ClaimInfoDBHelper.KEY_CAR_COLOR, 
				ClaimInfoDBHelper.KEY_CAR_BRAND, 
				ClaimInfoDBHelper.KEY_CAR_DETAIL, 
				ClaimInfoDBHelper.KEY_INSURANCE_PACKAGE
		};
		
		String selection = ClaimInfoDBHelper.KEY_ID + " = ?";
		String[] selectionArgs = new String[] {rowIdStr};
		return db.query(ClaimInfoDBHelper.DATABASE_TABLE, columnName, selection,selectionArgs, null, null, null);
	}
	
	public Cursor getLastRecord(){
//		String sql = "SELECT * FROM ? ORDER BY ? DESC LIMIT 1";
//		String[] selectionArgs = new String[] {
//				ClaimInfoDBHelper.DATABASE_TABLE,
//				ClaimInfoDBHelper.KEY_CLAIM_ID
//				};
		//String sql = "SELECT * FROM "+ ClaimInfoDBHelper.DATABASE_TABLE +" ORDER BY "+ ClaimInfoDBHelper.KEY_CLAIM_ID +" DESC LIMIT 1";
		
		String[] selectionArgs = new String[] {
				ClaimInfoDBHelper.DATABASE_TABLE
		};
		selectionArgs = null;
		String sql = "SELECT * FROM "+ ClaimInfoDBHelper.DATABASE_TABLE + " ORDER BY "+ ClaimInfoDBHelper.KEY_CLAIM_ID +" DESC LIMIT 1";
		
		return db.rawQuery(sql,selectionArgs);
	}
}